﻿using System;
using SystemDrawing = System.Drawing;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Logika;
using Neat;
using NeatStarter;
using System.Xml.Serialization;
using System.IO;

namespace RattlerRace
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : NeatGame
    {
        static GameOptions gameOptions;
        public static GameOptions GameOptions
        {
            get { return gameOptions; }
            set { gameOptions = value; }
        }

        static GameStats gameStats;

        public static GameStats GameStats
        {
            get { return gameStats; }
            set { gameStats = value; }
        }

        private static GameStatsList gameStatsList;
        static List<GameStats> results;
        public static List<GameStats> Results
        {
            get { return gameStatsList.Results; }
            set { gameStatsList.Results = value; }
        }


        public Game1()
        {
            GameWidth = 660;
            GameHeight = 660;
            GameBackgroundColor = Color.Black;

            gameOptions = new GameOptions();
            gameStats = new GameStats(3, 0, 1);
            results = new List<RattlerRace.GameStats>();
            FullScreen = false;
            gameStatsList = new GameStatsList();

        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            base.Initialize();
            HasConsole = true;
            ConsoleKey = Keys.OemTilde;
            Graphics.ApplyChanges();
            Window.Title = "NeatStarter";
            DefaultTransition = new Neat.Transitions.Fader();
            Console.AddCommand("greet", greet_func);
            //Console.AddCommand("g_start", g_start);
        }

        void greet_func(IList<string> args)
        {
            Console.WriteLine("Hello " + args[1] + "!");
        }

        void g_start(IList<string> args)
        {
            this.Console.Run("e_show gamescreen");
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            base.LoadContent();
            LoadTexture("Sprites\\GameThumbnail", "gameThumbnail");
            LoadTexture("Sprites\\appleRed", "appleRed");
            LoadTexture("Sprites\\appleGold", "appleGold");
            LoadTexture("Sprites\\snakep", "snake");
            LoadTexture("Sprites\\enemysnakep", "enemysnake");
            LoadFont(@"Sprites\Fonts\gamefont");
            LoadFont(@"Sprites\Fonts\menuFontZ");

            string path = @"Content\\results.xml";
            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(GameStatsList));

                StreamReader reader = new StreamReader(path);
                gameStatsList = (GameStatsList)serializer.Deserialize(reader);
                reader.Close();
            }
            catch (Exception e) { }
        }

        protected override void Behave(GameTime gameTime)
        {
            if (IsTapped(Keys.F11))
                Graphics.ToggleFullScreen();
            if (IsTapped(Keys.Pause))
                IsPaused = !IsPaused;
            base.Behave(gameTime);
        }

        protected override void Render(GameTime gameTime)
        {
            base.Render(gameTime);
        }

        public override void AddScreens()
        {
            base.AddScreens();
            Screens["gamescreen"] = new GameScreen(this);
            Screens["optionsscreen"] = new OptionsScreen(this);
            Screens["mainmenu"] = new SampleGUIScreen(this);
            Screens["gameoverscreen"] = new GameOverScreen(this);
            Screens["resultscreen"] = new GameResultScreen(this);
            Screens["startlevelscreen"] = new StartLevelScreen(this);
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        protected override void OnExiting(object sender, EventArgs args)
        {
            base.OnExiting(sender, args);
            XmlSerializer ser = new XmlSerializer(typeof(GameStatsList));

            using (FileStream fs = new FileStream(@"Content\\results.xml", FileMode.Create))
            {
                ser.Serialize(fs, gameStatsList);
            }
        }
    }
}
